package com.blockchain.coupon.controller;

import com.blockchain.coupon.entity.*;
import com.blockchain.coupon.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by flynn on 17-3-8.
 */
@Controller
@RequestMapping(value = "/merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    /**
     * 商户注册
     * 请求参数: 商户信息
     *          merchantAccount
     *          merchantPassword
     *          merchantName
     *          merchantLicenseNumber
     *          merchantAddress
     *          merchantLegalPersonName
     *          merchantBusinessScope
     * 响应结果: resultCode
     *              "1": 注册成功
     *              "2": 用户名已被注册
     *              "3": 注册失败
     *              "4": 用户正在审核中
     */
    @RequestMapping(value = "/register.action", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, String> merchantRegister(
            @RequestParam("account")  String account,
            @RequestParam("password") String password,
            @RequestParam("name") String name,
            @RequestParam("licence") String licence,
            @RequestParam("legalEntityName") String legalEntityName,
            @RequestParam("businessScope") String businessScope,
            @RequestParam("address") String address) {
        Merchant merchant = new Merchant();
        merchant.setMerchantAccount(account);
        merchant.setMerchantPassword(password);
        merchant.setMerchantName(name);
        merchant.setMerchantLicenseNumber(licence);
        merchant.setMerchantLegalPersonName(legalEntityName);
        merchant.setMerchantBusinessScope(businessScope);
        merchant.setMerchantAddress(address);
        Map<String, String> map = new HashMap<>();
        String resultCode = this.merchantService.merchantRegister(merchant);
        map.put("resultCode", resultCode);
        System.out.println(resultCode);
        return map;
    }

    /**
     * 商户登录
     * 请求参数: 商户信息
     *          merchantAccount
     *          merchantPassword
     * 响应结果: resultCode
     *              "1": 注册成功
     *              "2": 用户名不存在
     *              "3": 密码错误
     *              "4": 审核中
     */
    @RequestMapping(value = "/merchant_login.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, String> merchantLogin(String merchantAccount, String merchantPassword) {
        Map<String, String> map = new HashMap<>();
        String resultCode = this.merchantService.merchantLogin(merchantAccount, merchantPassword);
        map.put("resultCode", resultCode);
        return map;
    }

    /**
     * 修改商户信息
     * 请求参数:
     *          merchantId
     *          merchantName
     *          merchantDescription
     * 响应结果: resultCode
     *              "1": 修改成功
     *　　　　　     "0": 修改失败
     */
    @RequestMapping(value = "/merchant_info_modify.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, String> merchantInfoModify(String merchantId, String merchantName, String merchantDescription) {
        Map<String, String> map = new HashMap<>();
        String resultCode = this.merchantService.merchantInfoModify(merchantId, merchantName, merchantDescription);
        map.put("resultCode", resultCode);
        return map;
    }

    /**
     * 修改密码
     * 请求参数:
     *          merchantId
     *          merchantPassword
     *          merchantNewPassword
     * 响应结果: resultCode
     *              "1": 修改成功
     *　　　　　     "2": 原密码输入错误
     *              "3": 修改失败
     */
    @RequestMapping(value = "/merchant_password_modify.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, String> merchantPasswordModify(String merchantId, String merchantPassword, String merchantNewPassword) {
        Map<String, String> map = new HashMap<>();
        String resultCode = this.merchantService.merchantPasswordModify(merchantId, merchantPassword, merchantNewPassword);
        map.put("resultCode", resultCode);
        return map;
    }

    /**
     * 银行卡信息显示
     * 请求参数:
     *          merchantId
     * 响应结果: BankCard:
     *          bankcardId
     *          merchantName
     *          bankCardNumber
     *          bankOfDeposit
     */
    @RequestMapping(value = "/merchant_bank_card_info_display.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, Object> merchantBankCardInfoDisplay(String merchantId) {
        Map<String, Object> map = new HashMap();
        List<BankCard> list = this.merchantService.merchantBankCardInfoDisplay(merchantId);
        map.put("bankCardList", list);
        return map;
    }

    /**
     * 结算券申请界面显示
     * 请求参数:
     *          merchantId
     *          startTime
     *          endTime
     * 响应结果:
     *          merchantAccountBalance  商户账户余额
                clearingTicketTotalBalance 结算券账户余额
                申请记录列表　applylist
     */
    @RequestMapping(value = "/merchant_clearingTicket_apply_display.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, Object> merchantClearingTicketApplyDisplay(String merchantId, String startTime, String endTime) {
        Map<String, Object> map = this.merchantService.merchantClearingTicketApplyDisplay(merchantId, startTime, endTime);
        return map;
    }

    /**
     * 结算券申请
     * 请求参数:
     *          merchantId
                clearingTicketApplyAmount
     * 响应结果: resultCode
     *              "1" : 申请成功
         　　　　　　"0" : 申请失败
     */
    @RequestMapping(value = "/merchant_apply_clearingTicket.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, String> merchantApplyClearingTicket(String merchantId, String clearingTicketApplyAmount) {
        Map<String, String> map = new HashMap<>();
        String resultCode = this.merchantService.merchantApplyClearingTicket(merchantId, clearingTicketApplyAmount);
        map.put("resultCode", resultCode);
        return map;
    }

    /**
     * 结算券申请记录筛选
     * 请求参数:
     *          merchantId
     *          startTime
     *          endTime
     * 响应结果:
     *          申请记录列表applylist
     */
    @RequestMapping(value = "/merchant_apply_record_filtrate.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, Object> merchantApplyRecordFiltrate(String merchantId, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        List<ClearingTicketOperationLog> list = this.merchantService.merchantApplyRecordFiltrate(merchantId, startTime, endTime);
        map.put("applyList", list);
        return map;
    }

    /**
     * 优惠券发行界面显示
     * 请求参数:
     *          merchantId
     * 响应结果:
     *          clearingTicketTotalBalance
     */
    @RequestMapping(value = "/merchant_issue_coupon_display.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, String> merchantIssueCouponDisplay(String merchantId) {
        Map<String, String> map = new HashMap<>();
        String clearingTicketTotalBalance = this.merchantService.merchantIssueCouponDisplay(merchantId);
        map.put("clearingTicketTotalBalance", clearingTicketTotalBalance);
        return map;
    }

    /**
     * 优惠券发行
     * 请求参数:
     *          merchantId
     *          exceedAmount
     *          presentationAmount
     *          limitAmount
     *          isAccumulation
     *          issueTotalQuantity
     *          startTime
     *          endTime
     * 响应结果: resultCode
     *              "1" : 发行成功
     *　　　　　     "0" : 发行失败
     */
    @RequestMapping(value = "/merchant_issue_coupon.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, String> merchantIssueCoupon(String merchantId, @RequestBody CouponRule couponRule) {
        Map<String, String> map = new HashMap<>();
        String resultCode = this.merchantService.merchantIssueCoupon(merchantId, couponRule);
        map.put("resultCode", resultCode);
        return map;
    }

    /**
     * 优惠券发行管理界面
     * 请求参数:
     *          merchantId
     * 响应结果:
     *          发行规则 couponRule
     */
    @RequestMapping(value = "/merchant_issue_coupon_manage_display.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, Object> merchantIssueCouponManageDisplay(String merchantId) {
        Map<String, Object> map = new HashMap<>();
        CouponRule couponRule = this.merchantService.merchantIssueCouponManageDisplay(merchantId);
        map.put("couponRule", couponRule);
        return map;
    }

    /**
     * 优惠券终止发行
     * 请求参数:
     *          couponRuleId
     * 响应结果: resultCode
     *          "1":   终止成功
     　　　　　　"0":   终止失败
     */
    @RequestMapping(value = "/merchant_stop_issue_coupon.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, Object> merchantStopIssueCoupon(String couponRuleId) {
        Map<String, Object> map = new HashMap<>();
        String resultCode = this.merchantService.merchantStopIssueCoupon(couponRuleId);
        map.put("resultCode", resultCode);
        return map;
    }

    /**
     * 优惠券发行状态界面显示
     * 请求参数:
     *          merchantId
     * 响应结果:
     *          couponRule
     *          优惠券状态列表 list<CouponOperationLog>
     */
    @RequestMapping(value = "/merchant_issue_status_display.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, Object> merchantIssueStatusDisplay(String couponRuleId) {
        Map<String, Object> map  = this.merchantService.merchantIssueStatusDisplay(couponRuleId);
        return map;
    }

    /**
     * 优惠券发行状态筛选显示
     * 请求参数:
     *          merchantId
     * 响应结果:
     *          couponRule
     *          优惠券状态列表 list<CouponOperationLog>
     */
    @RequestMapping(value = "/merchant_stop_issue_status_filtrate.json", method = RequestMethod.POST,produces="application/json")
    @ResponseBody
    public Map<String, Object> merchantStopIssueStatusFiltrate(String merchantId, String startTimem, String endTime) {
        Map<String, Object> map = new HashMap<>();
        List<CouponOperationLog> list = this.merchantService.merchantStopIssueStatusFiltrate(merchantId, startTimem, endTime);
        map.put("statusList", list);
        return map;
    }

}
